use std::sync::OnceLock;

use global_hotkey::GlobalHotKeyManager;
use global_hotkey::hotkey::{Code, HotKey, Modifiers};
use once_cell::sync::Lazy;

pub(crate) static DEFAULT_ACTIVE_WINDOW_KEY: Lazy<HotKey> = Lazy::new(|| { HotKey::new(Some(Modifiers::SUPER | Modifiers::SHIFT), Code::Space) });

static mut MANAGER: OnceLock<GlobalHotKeyManager> = OnceLock::new();

pub(crate) fn register_global_hotkey() {
    // 注册全局快捷键
    unsafe {
        let result = MANAGER.set(GlobalHotKeyManager::new().unwrap());
        if result.is_err() {
            panic!("创建全局快捷键管理器失败")
        }
    }

    let manager = unsafe { MANAGER.get_mut().unwrap() };

    let result = manager.unregister(*DEFAULT_ACTIVE_WINDOW_KEY);
    if result.is_err() {
        log::error!("注销全局快捷键失败：{}", result.err().unwrap());
    }

    let result = manager.register(*DEFAULT_ACTIVE_WINDOW_KEY);
    if result.is_err() {
        log::error!("注册全局快捷键失败：{}", result.err().unwrap());
    }
}